package com.nobrain.uboard.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

public class SNSDataBase extends ContentProvider {

	private final static String AUTHORITY = "com.nobrain.uboard.provider";
	
	public final static Uri CONTENT_URI=Uri.parse("content://"+AUTHORITY);
	public final static Uri URI_USERINFO = Uri.withAppendedPath(CONTENT_URI, "userinfo");
	public final static Uri URI_FACEBOOK = Uri.withAppendedPath(CONTENT_URI, "facebook");
	
	private final static String DB_NAME = "uboard.db";
	
	private final static String[] DB_TABLES ={
		"userinfo",
		"facebook"
	};
	private final static UriMatcher URIMATCHER;
	
	static {
		URIMATCHER = new UriMatcher(UriMatcher.NO_MATCH);
		for(int i=0;i<DB_TABLES.length;i++){
			URIMATCHER.addURI(AUTHORITY, DB_TABLES[i], i);
		}
	}
	
	private final static int DB_VERSION = 1;
	
	private final static String[] CREATE_TABLE = {"create table if not exists "+DB_TABLES[0]+" (" +
			"_id integer not null primary key autoincrement," +
			"user text not null," +
			"title text not null," +
			"data01 text not null," +
			"data02 text not null" +
			");",
			
			"create table if not exists "+DB_TABLES[1]+" (" +
			"user text not null," +
			"title text not null," +
			"data01 text not null," +
			"data02 text not null" +
			");"
	};
	
	private SQLiteDatabase db;
	
	
	@Override
	public int delete(Uri uri, String where, String[] whereArgs) {
		String tableName = getTableName(uri);
		return db.delete(tableName, where, whereArgs);
	}

	@Override
	public String getType(Uri uri) {
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		try{
			String tableName = getTableName(uri);
			long rowId = db.insertOrThrow(tableName, null, values);
			if(rowId > 0){
				getContext().getContentResolver().notifyChange(CONTENT_URI, null);
				Uri mUri = ContentUris.withAppendedId(CONTENT_URI, rowId);
				return mUri;
			}
		}catch (SQLiteException e){
			return null;
		}
		return null;
	}

	@Override
	public boolean onCreate() {
		SNSDBOpenHelper helper = new SNSDBOpenHelper(getContext());
		if(helper == null){
			return false;
		}else{
			db = helper.getWritableDatabase();
			return true;
		}
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String where, String[] whereArgs, String orderBy) {
		String tableName = getTableName(uri);
		return db.query(tableName, projection, where, whereArgs, null, null, orderBy);
	}

	@Override
	public int update(Uri arg0, ContentValues values, String where, String[] whereArgs) {
		String tableName = getTableName(arg0);
		return db.update(tableName , values, where, whereArgs);
	}
	
	private String getTableName(Uri uri){
		int match = URIMATCHER.match(uri);
		return (match!=-1)?DB_TABLES[match]:null;
	}
	
	class SNSDBOpenHelper extends SQLiteOpenHelper{
		
		public SNSDBOpenHelper(Context context) {
			super(context, DB_NAME, null, DB_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			for(int i=0;i<CREATE_TABLE.length;i++){
				db.execSQL(CREATE_TABLE[i]);
			}
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			for(int i=0;i<DB_TABLES.length;i++){
				db.execSQL("drop table "+DB_TABLES[i]+";");
			}
			onCreate(db);
		}
		
	}
}
